

package com.dog.basics.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.sys.AdminRoleUpdateDto;
import com.dog.basics.bean.dto.admin.sys.AdminSysRoleAddDto;
import com.dog.basics.bean.dto.admin.sys.AdminSysRoleEditDto;
import com.dog.basics.bean.dto.admin.sys.AdminSysRoleFromDto;
import com.dog.basics.bean.model.sys.SysRole;
import com.dog.basics.bean.vo.admin.sys.AdminSysRoleVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.constant.CacheConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.service.sys.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author guonh
 * @date 2020-02-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/role")
@Api(value = "角色管理模块", tags = "角色管理模块")
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final MapperFacade mapperFacade;

    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return 角色信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询角色信息", notes = "sys_role_info")
    @PreAuthorize("@pms.hasPermission('sys_role_info')")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long")
    public R<AdminSysRoleVo> getById(@PathVariable Long id) {
        return R.ok(sysRoleService.getObjXq(id));
    }

    /**
     * 添加角色
     *
     * @param obj 角色信息
     * @return success、false
     */
    @SysLog("添加角色")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_role_add')")
    @ApiOperation(value = "添加角色", notes = "sys_role_add")
    @CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
    public R<Boolean> save(@Valid @RequestBody AdminSysRoleAddDto obj) {
        return R.ok(sysRoleService.saveAdminObj(obj));
    }

    /**
     * 修改角色
     *
     * @param obj 角色信息
     * @return success/false
     */
    @SysLog("修改角色")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_role_edit')")
    @ApiOperation(value = "修改角色", notes = "sys_role_edit")
    @CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
    public R<Boolean> update(@Valid @RequestBody AdminSysRoleEditDto obj) {
        return R.ok(sysRoleService.editAdminObj(obj));
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @SysLog("删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_role_del')")
    @ApiOperation(value = "删除角色", notes = "sys_role_del")
    @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "Long")
    @CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
    public R<Boolean> removeById(@PathVariable Long id) {
        return R.ok(sysRoleService.removeRoleById(id));
    }

    /**
     * 获取角色列表
     *
     * @return 角色列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取角色列表", notes = "sys_role_list")
    @PreAuthorize("@pms.hasPermission('sys_role_list')")
    public R<List<AdminSysRoleVo>> listRoles() {
        List<SysRole> list = sysRoleService.list(Wrappers.emptyWrapper());
        List<AdminSysRoleVo> newList = mapperFacade.mapAsList(list, AdminSysRoleVo.class);
        return R.ok(newList);
    }

    /**
     * 分页查询角色信息
     *
     * @param page 分页对象
     * @param obj  查询条件
     * @return 分页对象
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询角色信息", notes = "sys_role_page")
    @PreAuthorize("@pms.hasPermission('sys_role_page')")
    public R<IPage<AdminSysRoleVo>> getRolePage(PageParam page, AdminSysRoleFromDto obj) {
        return R.ok(sysRoleService.getAdminPageList(page, obj));
    }

    /**
     * 更新角色菜单
     *
     * @param obj 角色对象
     * @return success、false
     */
    @SysLog("更新角色菜单")
    @PutMapping("/menu")
    @ApiOperation(value = "更新角色菜单", notes = "sys_role_perm")
    @PreAuthorize("@pms.hasPermission('sys_role_perm')")
    public R<Boolean> saveRoleMenus(@RequestBody AdminRoleUpdateDto obj) {
        return R.ok(sysRoleService.updateRoleMenus(obj));
    }

    /**
     * 通过角色ID 查询角色列表
     *
     * @param roleIdList 角色ID
     * @return
     */
    @PostMapping("/getRoleList")
    @ApiOperation(value = "通过角色ID 查询角色列表", notes = "sys_role_lists")
    @ApiImplicitParam(name = "roleIdList", value = "角色ID(集合)", required = false, dataType = "List")
    @PreAuthorize("@pms.hasPermission('sys_role_lists')")
    public R<List<AdminSysRoleVo>> getRoleList(@RequestBody List<Long> roleIdList) {
        return R.ok(sysRoleService.findRolesByRoleIds(roleIdList, CollUtil.join(roleIdList, StrUtil.UNDERLINE)));
    }

}
